(function($){
    function initProvince() {
        var data = SysArea.province();
        var box = $('#province');
        box.find('option').each(function(i){
            if(i > 0) {
                $(this).remove();
            }
        });
        for(var i=0; i<data.length; i++) {
            box.append('<option value="'+ data[i] +'">'+ data[i] +'</option>');
        }
        $(box.find('option').get(0)).attr('selected', 'selected');
    }
    function initCity(p) {
        var data = SysArea.city(p);
        var box = $('#city');
        var ist = false;
        box.find('option').each(function(i){
            if(i > 0) {
                $(this).remove();
            }
        });
        if(p.substr(p.length - 1, 1) == '市') {
            data = [p];
            ist = true;
        }
        for(var i=0; i<data.length; i++) {
            box.append('<option value="'+ (ist ? 1 : data[i]) +'">'+ data[i] +'</option>');
        }
        $(box.find('option').get(0)).attr('selected', 'selected');
    }
    function initArea() {
        var pObj = $('#province');
        var cObj = $('#city');
        initProvince();
        pObj.on('change', function(){
            var o = $(this);
            initCity(o.val());
        });
    }
    var setFormResult = window.setFormResult = function (data) {
        //console.log(data);
        layer.closeAll();
        if(data.status != 10000) {
            layer.alert(data.errmsg, {icon: 5});
        } else {
            layer.confirm('添加成功！', {icon: 6, title:'系统提示', btn: '查看列表'}/*, function(index){
                location.reload(true); //继续添加
            }*/, function(index){
                location.href = $('#form_add').attr('durl');
            });
        }
    }
    $(function(){
        initArea();
        $('#form_add').on('valid.form', function(e, form){
            layer.load();
            form.submit();
        });
        $('#time').daterangepicker({
            "autoUpdateInput": false,
            "showDropdowns": true,
            "timePicker": true,
            "timePicker24Hour": true,
            "timePickerIncrement": 1,
            "autoApply": true,
            "locale": {
                "format": "YYYY-MM-DD HH:mm",
                "separator": " 至 ",
                "applyLabel": "确 定",
                "cancelLabel": "取 消",
                "fromLabel": "From",
                "toLabel": "To",
                "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
                "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                "firstDay": 1
            },
            "opens": "right",
            "drops": "down",
            "buttonClasses": "btn btn-sm",
            "applyClass": "btn-success",
            "cancelClass": "btn-default"
        }, function(start, end, label) {
        });

        $('#time').on('apply.daterangepicker', function(ev, picker) {
            $(this).val(picker.startDate.format('YYYY-MM-DD HH:mm') + ' 至 ' + picker.endDate.format('YYYY-MM-DD HH:mm'));
        });
        $('#time').on('cancel.daterangepicker', function(ev, picker) {
            $(this).val('');
        });
        
        
    });
})(jQuery);